<template>
  <div class="column-details-page w-75 mx-auto">
    <div class="column-info row mb-4 border-bottom pb-4 align-items-center" v-if="column">
      <div class="col-3 text-center">
        <img :src="column.avatar && column.avatar.url" :alt="column.title" class="rounded-circle border w-100">
      </div>
      <div class="col-9">
        <h4>{{column.title}}</h4>
        <p class="text-muted">{{column.description}}</p>
      </div>
    </div>
    <post-list :list="list"></post-list>
    <button class="btn btn-outline-primary mt-2 mb-5 mx-auto btn-block w-25" @click="loadMorePage" v-if="!isLastPage">
      加载更多
    </button>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { GlobalDataProps } from './../store'
import PostList from './../components/PostList.vue'
import useLoadMore from './../hooks/useLoadMore'
export default defineComponent({
  name: 'column-details',
  components: {
    PostList
  },
  setup () {
    const route = useRoute()
    const store = useStore<GlobalDataProps>()
    const currentId = route.params.id
    const column = computed(() => store.getters.getColumnById(currentId))
    const list = computed(() => store.getters.getPostById(currentId))
    const total = computed(() => store.state.post.total)
    const currentPage = computed(() => store.state.post.currentPage)
    onMounted(() => {
      store.dispatch('fetchColumn', currentId)
      store.dispatch('fetchPosts', { currentId: currentId, currentPage: 1, pageSize: 5 })
    })
    const { loadMorePage, isLastPage } = useLoadMore('fetchPosts', total, { currentPage: currentPage.value ? currentPage.value : 2, pageSize: 3 })
    return {
      column,
      list,
      loadMorePage,
      isLastPage
    }
  }
})
</script>
